package io.seata.samples.sc.tccat.consumer.action.impl;

import io.seata.samples.sc.tccat.consumer.action.TccActionTwo;
import io.seata.samples.sc.tccat.consumer.feign.TccActionTwoFeign;
import lombok.RequiredArgsConstructor;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.integration.tx.api.remoting.TwoPhaseResult;
import org.apache.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.stereotype.Service;

/**
 * The type Tcc action two.
 *
 * @author louis
 */
@RequiredArgsConstructor
@Service
public class TccActionTwoImpl implements TccActionTwo {
    private final TccActionTwoFeign tccActionTwoFeign;

    @Override
    public TwoPhaseResult prepare(String param) {
        String xid = RootContext.getXID();
        String result = ":::: TccActionTwo prepare, xid:" + xid + ", param:" + param;
        System.out.println(result);
        // RPC调用操作二服务
        String msg = tccActionTwoFeign.prepare(param);
        return new TwoPhaseResult(true, result + "\n" + msg);
    }

    @Override
    public TwoPhaseResult commit(BusinessActionContext actionContext) {
        String xid = actionContext.getXid();
        String result = ":::: TccActionTwo commit, xid:" + xid + ", param:" + actionContext.getActionContext("param");
        System.out.println(result);
        // RPC调用操作二服务
        String msg = tccActionTwoFeign.commit(xid);
        return new TwoPhaseResult(true, result + "\n" + msg);
    }

    @Override
    public TwoPhaseResult rollback(BusinessActionContext actionContext) {
        String xid = actionContext.getXid();
        String result = ":::: TccActionTwo rollback, xid:" + xid + ", param:" + actionContext.getActionContext("param");
        System.out.println(result);
        String msg = tccActionTwoFeign.rollback(xid);  // RPC调用操作二服务
        return new TwoPhaseResult(true, result + "\n" + msg);
    }
}
